﻿using System.Windows.Forms;
using System.Collections.Generic;

namespace NGridDrawer.Draw
{
    /// <LastUpdate author="Blaise Braye" Date="15/06/2008 17:05"/>
    /// <summary>
    /// Provide an indexed mapping for columns and rows of a grid of <see cref="DataGridView"/>
    /// </summary>
    /// <see cref="NGridDrawer.Draw.GridMapping"/>
    public class DataGridViewMapping : GridMapping
    {
        /// <summary>
        /// 	<para>Initializes an instance of the <see cref="DataGridViewMapping"/> class.</para>
        /// </summary>
        /// <param name="gridView">Grid to paint</param>
        /// <param name="rowsMap">
        /// Contains an ordered table of interesting rows indexes of the <paramref name="gridView"/></param>
        /// <param name="columnsMap">
        /// Contains an ordered table of interesting columns indexes of the <paramref name="gridView"/></param>
        public DataGridViewMapping(DataGridView gridView, int[] rowsMap, int[] columnsMap)
            : base(rowsMap, columnsMap)
        {
            this.gridView = gridView;
        }

        private DataGridView gridView { get; set; }

        /// <summary>
        /// Returns the <see cref="DataGridViewRow"/> object 
        /// of which index is stored in the <paramref name="rowKey"/> position of the rows mapping
        /// </summary>
        /// <param name="rowKey">
        /// </param>
        /// <returns>
        /// </returns>
        public DataGridViewRow Row(int rowKey)
        {
            return gridView.Rows[RowIndex(rowKey)];
        }

        public IEnumerable<DataGridViewRow> Rows
        {
            get
            {
                for (int i = 0; i < RowCount; i++)
                {
                    yield return(Row(i));
                }
            }
        }

        /// <summary>
        /// Returns the <see cref="DataGridViewColumn"/> object 
        /// of which index is stored in the <paramref name="columnKey"/> position of the columns mapping
        /// </summary>
        /// <param name="columnKey">
        /// </param>
        /// <returns>
        /// </returns>
        public DataGridViewColumn Column(int columnKey)
        {
            return gridView.Columns[ColumnIndex(columnKey)];
        }


        /// <summary>
        /// Returns the <see cref="DataGridViewCell"/> object 
        /// of which row index is stored in the <paramref name="rowKey"/> position of the rows mapping
        /// and column index is stored in the <paramref name="columnKey"/> position of the columns mapping
        /// </summary>
        /// <param name="rowKey">
        /// </param>
        /// <param name="columnKey">
        /// </param>
        /// <returns>
        /// </returns>
        public DataGridViewCell Cell(int rowKey, int columnKey)
        {
            return gridView[ColumnIndex(columnKey), RowIndex(rowKey)];
        }
    }
}